<?php
	// thinhnc 2013-04-09
	// Nhan cac tham so ngaybatdau, tkb, hstart, hend va sobuoi de tim kiem giao vien co lich ranh phu hop
	if(	isset($_GET['f_ngaybatdau']) && 
		isset($_GET['f_tkb']) && 
		isset($_GET['f_hstart']) && 
		isset($_GET['f_hend']) && 
		isset($_GET['f_sobuoi']))
	{
		require_once 'class/functions.php';
		require_once 'class/class.giangvien.php';
		require_once 'class/class.phieuthu.php';
		$giangvien = new giangvien();		
		$phieuthu = new phieuthu();
		include 'connect/connect.php';
		$query = mysql_query('SELECT * FROM tbl_lichranhgv LEFT JOIN tbl_giangvien ON  tbl_lichranhgv.gvid = tbl_giangvien.id
							ORDER BY f_tinnhiem DESC'); // sap xep theo diem tin nhiem
		
		$tkb = $giangvien->getTKB($_GET['f_tkb'], $_GET['f_hstart'], $_GET['f_hend']);
		
		$ngaybatdau = str_replace("/","-",$_GET['f_ngaybatdau']);
		$ngayketthuc = $phieuthu->tinhNgayKetThuc($_GET['f_ngaybatdau'], $_GET['f_sobuoi'], $_GET['f_tkb'], $_GET['f_hstart'], $_GET['f_hend']);
// echo "Ngayketthuc: ".$ngayketthuc."<br />TKB: ";
// foreach ($tkb as $t)
// echo $t.", ";
// echo "<br />"."CARID = ";
		$dsGV = array();
		while ($rows = mysql_fetch_array($query))
		{
// echo ", ".$rows['carid'];
			if(in_array($rows['carid'], $tkb))
			{		
// echo " [pass]";			
				$query2 = mysql_query("SELECT * FROM tbl_lichphanconggv WHERE gvid = '".$rows['gvid']."' AND carid = '".$rows['carid']."'");
				$numrow = mysql_num_rows($query2);
				if ($numrow > 0) // neu record da ton tai trong table: so sanh ngay
				{
					while ($subrow = mysql_fetch_array($query2))
					{
						if ( ($subrow['carid'] == $rows['carid']) 
						&& ( (soSanhNgay($subrow['taken_on'], $ngaybatdau) == 1 && soSanhNgay($subrow['taken_on'], $ngayketthuc) == 1)
							|| (soSanhNgay($ngaybatdau, $subrow['available_on']) == 1 && soSanhNgay($ngayketthuc, $subrow['available_on']) == 1)
						))
						{
							$dsGV[$rows['gvid']]['gvid'] = $rows['gvid'];
							$dsGV[$rows['gvid']]['hovaten'] = $rows['f_hovaten'];
							$dsGV[$rows['gvid']]['car'][] = (int)$rows['carid'];
						}
// else
// echo " [taken]";
					}
				}
				else { // new record chua ton tai trong table: dua vao mang $dsGv
// echo " [added]";
					$dsGV[$rows['gvid']]['gvid'] = $rows['gvid'];
					$dsGV[$rows['gvid']]['hovaten'] = $rows['f_hovaten'];
					$dsGV[$rows['gvid']]['car'][] = (int)$rows['carid'];
				}
			}
		}
				
		foreach ($dsGV as $gv)
		{
			$soSanh = count(array_intersect($tkb,$gv['car']));
// echo "<br />CarGV: ";
// foreach ($gv['car'] as $x)
	// echo $x.", ";
// echo "<br />";			
			// $query3 = mysql_query("INSERT INTO tbl_lichphanconggv(gvid,carid,taken_on,available_on) VALUES ('".$rows['gvid']."','".$rows['carid']."','".$ngaybatdau."','".$ngayketthuc."')") or die(mysql_error());
			// if ($query3)
			// echo "inserted!<br />";
			// else
			// echo "insert failed.<br />";
			if($soSanh == count($tkb))
			{				
				echo "<option value='".$gv['gvid']."'>";
				echo $gv['hovaten'];
				echo "</option>";
				// echo "<pre>";
				// print_r($gv);
				// echo "</pre>";
			}
		}
		echo "<option value='0' selected='selected'>";
		echo "Chưa sắp lớp";
		echo "</option>";
	}
?>